package cn.baiweigang.uu.api.linearswap;

import cn.baiweigang.uu.api.BaseApi;
import cn.baiweigang.uu.constants.BtcConfig;
import cn.baiweigang.uu.enums.*;
import cn.baiweigang.uu.model.TradeInfo;
import cn.baiweigang.uu.msg.MsgPublisher;
import cn.baiweigang.uu.msg.OrderChangeEvent;
import cn.baiweigang.uu.utils.DateUtil;
import cn.baiweigang.uu.utils.LogUtil;
import cn.baiweigang.uu.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.huobi.linearswap.wss.event.OrdersSubResponse;
import com.huobi.linearswap.wss.handle.WssNotificationHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class OrderNotificationWs extends BaseApi {
    private String host = BtcConfig.HB_HOST;
    private String url = "/linear-swap-notification";

    @Autowired
    private MsgPublisher msgPublisher;

    public boolean sub(String accountName, ContractCodeEnum contractCode) {
        loadByAccountName(accountName);
        WssNotificationHandle wssNotificationHandle = new WssNotificationHandle(host, url,
                apiKeyInfo.getApiKey(), apiKeyInfo.getSecretKey());
        try {
            List<String> channels = Lists.newArrayList();
            channels.add("orders." + contractCode.getCode());
            wssNotificationHandle.sub(channels, response -> {
                LogUtil.logDebug("收到服务端推送的订单信息 ");
                LogUtil.logDebug(response);
                if (StringUtil.isNotBlank(response)) {
                    OrdersSubResponse order = JSON.parseObject(response, OrdersSubResponse.class);
                    TradeInfo tradeInfo = new TradeInfo();
                    tradeInfo.setAccountName(accountName);
                    tradeInfo.setContractCode(ContractCodeEnum.byCode(order.getContractCode()));
                    tradeInfo.setVolume(order.getVolume().longValue());
                    tradeInfo.setPrice(order.getPrice());
                    tradeInfo.setTradeAvgPrice(order.getTradeAvgPrice());
                    tradeInfo.setIstpsl(order.getIstpsl());
                    tradeInfo.setOrderPriceType(OrderPriceTypeEnum.byCode(order.getOrderPriceType()));
                    tradeInfo.setDirection(DirectionEnum.byCode(order.getDirection()));
                    tradeInfo.setOffset(OffsetEnum.byCode(order.getOffset()));
                    tradeInfo.setStatus(TradeStatusEnum.byCode(order.getStatus()));
                    tradeInfo.setOrderId(order.getOrderId());
                    tradeInfo.setOrdeIdStr(order.getOrderIdStr());
                    tradeInfo.setClientOrderId(order.getClientOrderId() + "");
                    tradeInfo.setOrderSource(order.getOrderSource());
                    tradeInfo.setCreatedGmt(order.getCreatedAt());
                    tradeInfo.setTradeVolume(order.getTradeVolume());
                    tradeInfo.setTradeTurnover(order.getTradeTurnover());
                    tradeInfo.setFee(order.getFee());
                    tradeInfo.setTradeAvgPrice(order.getTradeAvgPrice());
                    tradeInfo.setMarginFrozen(order.getMarginFrozen());
                    tradeInfo.setProfit(order.getProfit());
                    tradeInfo.setCanceledGmt(order.getCanceledAt());
                    tradeInfo.setMarginMode(MarginModeEnum.byCode(order.getMarginMode()));
                    tradeInfo.setRealProfit(order.getRealProfit());

                    tradeInfo.setDealDateTime(DateUtil.formatDateYMDHMS(new Date(tradeInfo.getCreatedGmt())));

                    OrderChangeEvent orderChangeEvent = new OrderChangeEvent("OrderNotificationWs", tradeInfo);
                    LogUtil.logInfo("收到ws发来的订单信息，准备发内部消息");
                    msgPublisher.publish(orderChangeEvent);
                }

            });
            return true;
        } catch (Throwable e) {
            LogUtil.logError(e);
        }
        return false;
    }
}
